<template>
  <div class="casenodept-page">
    <el-row>
      <el-col :span="6">
        <div class="grid-content">
          <div class="title">
            <div style="float: left">组织机构</div>
            <el-input
              v-model="deptFilter"
              @input="filterDept"
              placeholder="搜索"
              suffix-icon="fa fa-search"
              style="float: right;width: calc(100% - 80px);height: 33px"
            />
          </div>
          <el-tree
            ref="deptTree"
            :data="data"
            :default-expanded-keys="shidata"
            :props="defaultProps"
            node-key="id"
            highlight-current
            :filter-node-method="filterNode"
            @node-click="handleNodeClick"
            style="height: calc(100% - 45px);overflow:auto;"
          />
        </div>
      </el-col>
      <el-col :span="18" style="padding-left: 10px">
        <div class="grid-content">
          <div class="title" slot="header">
            <div style="float: left">案件字</div>
          </div>
          <el-form :inline="true" ref="form" :model="form"  style="margin-top:50px">
            <el-form-item prop="casenoWord" label="案件字">
              <el-input v-model="form.casenoWord"></el-input>
            </el-form-item>
            <el-form-item prop="casenoLength" label="案件顺序号位数">
              <el-input v-model="form.casenoLength"></el-input>
            </el-form-item>
            <el-form-item prop="casenoStart" label="案件顺序号起始号">
              <el-input v-model="form.casenoStart"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="add">保存</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import utils from "@/assets/utils/zhzf-util";
import deptApi from "@/api/system/dept";
import casenoDeptApi from "@/api/system/casenoDept";
import HeadBtns from "../../comps/zhzf/head-btns";

export default {
  name: "casenoDeptPage",
  components: { HeadBtns },
  data() {
    return {
      form: {
        id: "",
        deptId: "",
        casenoWord: "",
        casenoLength: "",
        casenoStart: ""
      },
      deptFilter: "",
      data: [],
      shidata: [],
      dept: {},
      defaultProps: {
        children: "children",
        label: "name"
      },
      selected: []
    };
  },
  mounted() {
    this.initPage();
  },
  methods: {
    filterDept() {
      this.$refs.deptTree.filter(this.deptFilter);
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    async initPage(param) {
      let dataList = await this._fetchData();
      this.loadListTree(dataList);
    },
    loadListTree(dataList) {
      let treeList = utils.generatorDeptTree(dataList);
      this.data = treeList.treeList;

      this.shidata.push(treeList.treeList[0].id);
    },
    /**
     * 查询数据
     * @param param
     * @returns {Promise<*>}
     */
    async _fetchData(param) {
      param = Object.assign({ type: 1 }, param || {});
      let res = await deptApi.deptPage(param);
      return res.data;
    },
    handleNodeClick(data) {
      this.form = {
        id: "",
        deptId: "",
        casenoWord: "",
        casenoLength: "",
        casenoStart: ""
      };
      this.form.deptId = data.id;
      this.getFormData(data.id);
    },
    getFormData(deptId) {
      casenoDeptApi.selectByDeptId({ id: deptId }).then(res => {
        if (res.data) {
          var data = res.data;
          this.form = data;
        }
      });
    },
    add() {
      if (this.form.id) {
        casenoDeptApi.mod({ ...this.form }).then(data => {
          this.$emit("submit");
        });
      } else {
        casenoDeptApi.add({ ...this.form }).then(data => {
          this.$emit("submit");
        });
      }
    }
  }
};
</script>

<style scoped lang="scss">
.dept-page {
  height: calc(100% - 18px) !important;

  .grid-content {
    height: 100%;
    background: #fff;

    .d2-crud {
      height: 100% !important;
    }
  }

  .el-row,
  .el-col {
    height: 100%;
  }

  &:last-child {
    margin-bottom: 0;
  }
}
</style>
